package com.smartcommunity.service.villageGroup.impl;

import com.smartcommunity.bean.entity.Camera;
import com.smartcommunity.dao.villageGroup.VillageGroupDao;
import com.smartcommunity.entity.ac.AccessDevice;
import com.smartcommunity.entity.villagegroup.VillageGroup;
import com.smartcommunity.entity.villagegroup.VillageGroupDevice;
import com.smartcommunity.service.villageGroup.VillageGroupService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class VillageGroupServiceImpl implements VillageGroupService {

    @Autowired
    private VillageGroupDao villageGroupDao;

    @Override
    public void addGroup(VillageGroup villageGroup) {
        villageGroup.setMrowtime(new Date());
        villageGroupDao.addGroup(villageGroup);
    }

    @Override
    public void removeGroup(Long id) {
        villageGroupDao.removeGroup(id);
        List list = new ArrayList<Long>();
        list.add(id);
        villageGroupDao.deleteDeviceByGroup(id);
        villageGroupDao.deleteCameraByGroup(id);
        villageGroupDao.deleteDevice(null,list);
    }

    @Override
    public void updateGroup(VillageGroup villageGroup) {
        villageGroup.setMrowtime(new Date());
        villageGroupDao.updateGroup(villageGroup);
    }

    @Override
    public void saveGroup(List<VillageGroup> villageGroups) {
        if(!CollectionUtils.isEmpty(villageGroups)){
            for(VillageGroup villageGroup:villageGroups){
                villageGroup.setMrowtime(new Date());
                if(villageGroup!=null){
                    villageGroupDao.updateGroup(villageGroup);
                }else{
                    villageGroupDao.addGroup(villageGroup);
                }

                if(!CollectionUtils.isEmpty(villageGroup.getCameraList())){
                    List<String> idList = new ArrayList<>();
                    List<VillageGroupDevice> list = new ArrayList<>();
                    for(Camera device:villageGroup.getCameraList()){
                        idList.add(device.getCameraId());
                        VillageGroupDevice groupDevice = new VillageGroupDevice();
                        groupDevice.setDeviceId(device.getCameraId());
                        groupDevice.setGroupId(villageGroup.getId());
                        groupDevice.setType(device.getCameraType().toString());
                        list.add(groupDevice);
                    }
                    villageGroupDao.deleteDevice(idList,null);
                    villageGroupDao.batchAddGroupDevice(list);
                }
                if(!CollectionUtils.isEmpty(villageGroup.getDeviceList())){
                    List<VillageGroupDevice> list = new ArrayList<>();
                    List<String> idList = new ArrayList<>();
                    for(AccessDevice device:villageGroup.getDeviceList()){
                        idList.add(device.getDeviceId());
                        VillageGroupDevice groupDevice = new VillageGroupDevice();
                        groupDevice.setDeviceId(device.getDeviceId());
                        groupDevice.setGroupId(villageGroup.getId());
                        groupDevice.setType(device.getDeviceType().toString());
                        list.add(groupDevice);
                    }
                    villageGroupDao.deleteDevice(idList,null);
                    villageGroupDao.batchAddGroupDevice(list);
                }
            }
        }
    }



    @Override
    public void delDeviceByGroup(List<Long> groupId) {
        villageGroupDao.deleteDevice(null,groupId);
    }

    @Override
    public void delDeviceByDevice(List<String> deviceIds) {
        villageGroupDao.deleteDevice(deviceIds,null);
    }
}
